package terra.network.packet.server;

import org.jboss.netty.buffer.ChannelBuffer;

import terra.network.packet.Packet;

public class ChatMessage extends Packet {

	public String message;
	public int oId;
	
	public ChatMessage(int oId,String msg) {
		this.oId = oId;
		message = msg;
	}

	public ChatMessage() {
	}

	@Override
	public ChannelBuffer toBuffer() {
		byte[] bytes = stringToBytes(message);
		ChannelBuffer buf = getNewBuffer(1+8+bytes.length);
		buf.writeByte(getId());
		buf.writeInt(bytes.length+4);
		buf.writeInt(oId);
		buf.writeBytes(bytes);
		bytes = null;
		return buf;
	}

	@Override
	public Packet fromBuffer(ChannelBuffer buf) {
		int length = buf.readInt()-4;
		oId =  buf.readInt();
		byte[] bytes = new byte[length];
		buf.readBytes(bytes);
		message = bytesToString(bytes);
		return this;
	}

	@Override
	public int getLength() {
		return 0;
	}

	@Override
	public boolean isDynamicLength() {
		return true;
	}

	@Override
	public byte getId() {
		return 8;
	}
	
	@Override
	public byte getPriority() {
		return PRIOR_NORMAL;
	}
}
